/***************************************************************************
 $ @Description: 
 $ @Author: NavyPeng
 $ @Date: 2024-04-12 09:05:53
 $ @LastEditTime: 2024-04-19 15:10:44
 $ @LastEditors: NavyPeng
 ***************************************************************************/
#include "Ctrl.h"


#if CompositePattern_

#include <list>
#include <iomanip>

class Component
{
protected:
	string name;
public:
	Component(string _name) {
		name = _name;
	}
	virtual void Add(Component* c) = 0;
	virtual void Remove(Component* c) = 0;
	virtual void Display(int depth) = 0;
};

class Leaf : public Component
{
public:
	Leaf(string _name) : Component(_name) {

	}
	void Add(Component* c) override {
		cout << "Cannot add to a leaf" << endl;
	}
	void Remove(Component* c) override {
		cout << "Cannot remove to a leaf" << endl;
	}
	void Display(int depth) override {
		for (int i = 0; i < depth; i++) {
			cout << "-";
		}
		cout << name << endl;
	}
};

class Composite : public Component
{
private :
	list<Component*>* children = new list<Component*>();

public:
	Composite(string _name) : Component(_name) {
	
	}

	void Add(Component* c) override {
		children->push_back(c);
	}
	void Remove(Component* c) override {
		children->remove(c);
	}
	void Display(int depth) override {
		for (int i = 0; i < depth; i++) {
			cout << "-";
		}
		cout << name << endl;
		for (auto component = children->begin(); component != children->end(); component++) {
			(*component)->Display(depth + 1);
		}
	}
};


int main() {
	Composite* root = new Composite("root");
	root->Add(new Leaf("Leaf A"));
	root->Add(new Leaf("Leaf B"));

	Composite* comp = new Composite("Composite X");
	comp->Add(new Leaf("Leaf XA"));
	comp->Add(new Leaf("Leaf XB"));

	root->Add(comp);

	Composite* comp2 = new Composite("Composite XY");
	comp2->Add(new Leaf("Leaf XYA"));
	comp2->Add(new Leaf("Leaf XYB"));

	comp->Add(comp2);

	root->Add(new Leaf("Leaf C"));

	Leaf* leaf = new Leaf("Leaf D");
	root->Add(leaf);
	root->Remove(leaf);

	root->Display(1);




}



#endif